/**
 * 申请代理 js 脚本
 * Created by hejx on 2017/6/28.
 */
app.controller('applyProxyController', function($scope,$http) {

    $scope.URL = {
        GENINVITECODE_URL:"/admin/user/diamond/proxy/genInviteCode",
        SUBMIT_URL:"/admin/user/diamond/proxy/add"
    };

    $scope.user = {
        playerId:"",
        phone:"",
        inviteCode:"",
        realname:""
    };

    //校验参数 return boolean
    $scope.validateParam = function(){
        if(!validatemobile($scope.user.phone)){
            layer.msg(common.ERR_MSG.PHONE_ERROR_MSG);
            return true;
        }
        if(isNull($scope.user.inviteCode)){
            layer.msg("请生成邀请码");
            return true;
        }
        return false;
    }

    //save操作
    $scope.save = function(){

        if($scope.validateParam()){
            return false;
        }

        $('#subBtn')[0].disabled = true;
        //提交数据
        $http({method : 'POST', data:$scope.user, url : $scope.URL.SUBMIT_URL})
            .success(function(data,status,headers,config){
                console.log(data);
                $('#subBtn')[0].disabled = false;
                if(data.code == 0){ // 新增成功
                    alert("创建成功");
                    location.reload();
                }else{
                    layer.msg(data.msg);
                }
            }).error(function(data,status,headers,config){
            $('#subBtn')[0].disabled = false;
            console.log('error..........');
            console.log(data);
        });
    };

    $scope.playerMsg = null; //错误信息
    $scope.playerInfo = null; //玩家信息

    //查询玩家信息模块
    $scope.serchPlayerInfo = {
        serch : function(){

            if(isNull($scope.user.playerId)){
                layer.msg("请输入玩家ID");
                return false;
            }

            $.ajax({
                url : '/admin/gm/serchRoleInfo',
                type:'GET', //GET
                async:false,    //或false,是否异步
                data:{
                    uid:$scope.user.playerId
                },
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                success:function(data,textStatus,jqXHR){
                    if(data.code == 1){
                        //查询到指定ID的角色信息
                        $scope.playerMsg = null;
                        $scope.serchPlayerInfo.playerInfo(data.data);
                    }else{
                        $scope.playerMsg = "没有查询到ID为 "+$scope.user.playerId+" 的玩家信息";
                        $scope.playerInfo = null;
                    }
                },
                error:function(xhr,textStatus){
                    console.error('错误')
                    console.error(xhr)
                    console.error(textStatus)
                }
            })
        },
        playerInfo : function(role){
            console.log(role);
            $scope.playerInfo = "玩家昵称:" + role.name;
        }
    };

    /**
     * 生成邀请码
     */
    $scope.genInviteCode = function(){
        if(isNull($scope.user.playerId)){
            layer.alert("玩家ID不能为空", {
                icon: 5,
                skin: 'layer-ext-moon'
            })
            return;
        }
        //提交数据
        $http({method : 'GET', data:{playerId:$scope.user.playerId}, url : $scope.URL.GENINVITECODE_URL+
        "?playerId="+$scope.user.playerId}).success(function(data,status,headers,config){
                if(data.code==0){
                    $scope.user.inviteCode = data.data;
                    $('#getInviteCode')[0].disabled = true;
                }else{
                    layer.alert(data.msg, {
                        icon: 5,
                        skin: 'layer-ext-moon'
                    })
                }
            }).error(function(data,status,headers,config){
            console.log('error..........');
            console.log(data);
        });
    };


});